<?php
class User extends Admin_Controller
{
	public function __construct()
	{
		parent::__construct();
		
		$this->load->library('Table');
		$this->load->library('Pagination');
		
		$this->load->helper('form');
	}
	
	public function index()
	{
		$this->users();
	}
	//管理用户
	public function users($infos = '', $uid = '')
	{
		$this->load->model('user/users', 'users');
		if ($infos == 'reset_pass' && $uid)
		{
			$data['reset_message'] = "<script>alertMsg.error('发送失败！')</script>";
			if ($query = $this->users->get_user_by_id($uid) AND $query->num_rows() == 1)
			{				
				$user = $query->row();

				// 创建新密码 并发送邮件
				if ($this->user_login->forgot_password($user->username))
				{
					// Query once again, because the database is updated after calling forgot_password.
					$query = $this->users->get_user_by_id($uid);
					// Get user record
					$user = $query->row();

					// Reset the password
					if ($this->user_login->reset_password($user->username, $user->newpass_key))
					{							
						$data['reset_message'] = "<script>alertMsg.correct('发送成功！')</script>";
					}
				}
			}
		}
		else
		{
			if ($infos == 'ban')
			{
				$this->users->ban_user($uid);
			}
			else
			{
				$this->users->unban_user($uid);
			}
		}

		$page = max(1, intval($this->input->post('pageNum')));
		$limit = 18;
		$offset = ($page - 1) * $limit;
		
		// Get all users
		$data['users'] = $this->users->get_all($offset, $limit)->result();
		//分页
		$data['totalCount'] = $this->users->get_all()->num_rows();
		$data['numPerPage'] = $limit;
		$data['currentPage'] = $page;

		$this->load->view('users', $data);
	}
	
	public function unactivated_users()
	{
		$this->load->model('user/user_temp', 'user_temp');
		
		/* Database related */
		
		// If activate button pressed
		if ($this->input->post('ids'))
		{
			$array = array(
				'statusCode'=>'200',
				'message'=>'修改成功',
				'navTabId'=>'',
				'rel'=>'',
				'callbackType'=>'',
				'forwardUrl'=>'',
				'confirmMsg'=>''
			);

			$ids = explode(",",$this->input->post('ids'));
			// Search checkbox in post array
			foreach ($ids as $value)
			{
				// Check if user exist, $value is username
				if ($query = $this->user_temp->get_login($value) AND $query->num_rows() == 1)
				{
					// Activate user
					$this->user_login->activate($value, $query->row()->activation_key);
				}
			}
			$this->output->set_output(json_encode($array));
		}
		else
		{
		/* Showing page to user */
			$page = max(1, intval($this->input->post('pageNum')));
			$limit = 18;
			$offset = ($page - 1) * $limit;
			
			// Get all users
			$data['users'] = $this->user_temp->get_all($offset, $limit)->result();
			//分页
			$data['totalCount'] = $this->user_temp->get_all()->num_rows();
			$data['numPerPage'] = $limit;
			$data['currentPage'] = $page;

			$this->load->view('unactivated_users', $data);
		}
	}
	public function email_send()
	{
		$this->load->model('user/user_temp', 'user_temp');
		$email = $this->input->get('email');
		$new_user = $this->user_temp->get_user_by_email($email)->row_array();
		if ($new_user)
		{
			$this->user_login->_sending_activation_email($new_user);
			$this->_jui_message('发送成功');
		}
	}
	public function roles($type = '')
	{		
		$this->load->model('user/roles', 'roles');
					
		if ($type == 'add')
		{
			if ($this->input->post('role_name'))
			{
				$this->roles->create_role($this->input->post('role_name'), $this->input->post('role_parent'));
				$this->_jui_message('操作成功');
			}
			else
			{
				$data['roles'] = $this->roles->get_all()->result();
				$this->load->view('roles_add', $data);
			}
		}
		else if ($type == 'delete')
		{
			$ids = explode(",",$this->input->post('ids'));
			// Loop trough $_POST array and delete checked checkbox
			foreach ($ids as $value)
			{
				$this->roles->delete_role($value);			
			}
			$this->_jui_message('操作成功');
		}
		else
		{

			$data['roles'] = $this->roles->get_all()->result();
			$this->load->view('roles', $data);
		}
	}
	
	public function uri_permissions($role_id = 1)
	{
		function trim_value(&$value) 
		{ 
			$value = trim($value); 
		}

		$this->load->model('user/permissions', 'permissions');
		
		if ($this->input->post('allowed_uris'))
		{
			// Convert back text area into array to be stored in permission data
			$allowed_uris = explode("\n", $this->input->post('allowed_uris'));
			
			// Remove white space if available
			array_walk($allowed_uris, 'trim_value');
		
			// Set URI permission data
			// IMPORTANT: uri permission data, is saved using 'uri' as key.
			// So this key name is preserved, if you want to use custom permission use other key.
			$this->permissions->set_permission_value($role_id, 'uri', $allowed_uris);
			$this->_jui_message('操作成功');
		}
		else
		{
			$allowed_uris = $this->permissions->get_permission_value($role_id, 'uri');

			if ( ! empty($allowed_uris))
			{
				$allowed_uris = implode("\n", $allowed_uris);
			}

			$data['allowed_uris'] = $allowed_uris;

			$this->load->view('uri_permissions', $data);
		}
	}
	
	public function custom_permissions($role_id = 1)
	{
		$this->load->model('user/permissions', 'permissions');

		if ($this->input->post('submit') == 'ok')
		{
			// Note: Since in this case we want to insert two key with each value at once,
			// it's not advisable using set_permission_value() function						
			// If you calling that function twice that means, you will query database 4 times,
			// because set_permission_value() will access table 2 times, 
			// one for get previous permission and the other one is to save it.
			
			// For this case (or you need to insert few key with each value at once) 
			// Use the example below
		
			// Get role_id permission data first. 
			// So the previously set permission array key won't be overwritten with new array with key $key only, 
			// when calling set_permission_data later.
			$permission_data = $this->permissions->get_permission_data($role_id);
		
			// Set value in permission data array
			$permission_data['edit'] = $this->input->post('edit');
			$permission_data['delete'] = $this->input->post('delete');
			
			// Set permission data for role_id
			$this->permissions->set_permission_data($role_id, $permission_data);

			$this->_jui_message('操作成功');
		}
		else
		{
			// Get edit and delete permissions
			$data['edit'] = $this->permissions->get_permission_value($role_id, 'edit');
			$data['delete'] = $this->permissions->get_permission_value($role_id, 'delete');
		
			// Load view
			$this->load->view('custom_permissions', $data);
		}
	}

	//删除用户
	public function del_user()
	{
		$this->load->model('user/users', 'users');
		$this->load->model('user/user_autologin', 'autologin');
		$this->load->model('user/user_guanzhu', 'guanzhu');
		$this->load->model('user/user_like', 'like');
		$this->load->model('user/user_favorite', 'favorite');
		$this->load->model('Caidan_mdl');
		$this->load->model('Comments_mdl');

		$id = $this->input->get('id',TRUE);
		$this->users->delete_user($id);
		$this->autologin->delete_user_id($id);
		$this->guanzhu->del_user($id);
		$this->like->del_user_id($id);
		$this->favorite->del_user_id_caidan($id);
		$this->favorite->del_user_id_caipu($id);
		$this->Caidan_mdl->delete_caidan_uid($id);
		$this->Comments_mdl->remove_cmt(array('authorId' => $id));
		$this->_jui_message('操作成功');
	}
	//删除未激活用户
	public function del_user_temp()
	{
		$this->load->model('user/user_temp', 'user_temp');
		$id = $this->input->get('id',TRUE);
		$this->user_temp->delete_user($id);
		$this->_jui_message('操作成功');
	}
	//闲置
	public function _fetch_id_as_array()
	{
		$id = $this->input->get('id',TRUE);

		if(empty($id))
		{
			$id = $this->input->post('id',TRUE);
		}

		return $id ? (is_array($id) ? $id : array($id)) : array();
	}
}

/* End of file backend.php */
/* Location: ./application/controllers/manage/backend.php */